const cloud = require('wx-server-sdk')
cloud.init({env: "ldwy-7gyte0j599dd1c70"})
const db = cloud.database()
var request = require('request-promise');
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  if (event.type == "getOpenId") {
   return wxContext.OPENID;
  } 
  if (event.type == "getNotice") {
    res = await db.collection("notice").where({
      _id: "b00064a7606996490d38d01a7094fb3b"
    }).get()
    return res;
   }if (event.type == "post") {
    res = await db.collection("task").add({data: event.data})
  } if (event.type == "gettask") {
    res = await db.collection("task").orderBy('upTime', 'desc').where({openId:wxContext.OPENID}).get()
    return res
  } 
  if (event.type == "getAllTask") {
    res = await db.collection("task").orderBy('upTime', 'desc').get()
    return res
  } 
  if (event.type == "deleteTask") {
    cloud.deleteFile({
      fileList: [event.fileId]
    })
    res = await db.collection("task").where({
      _id: event.id
    }).remove()

  }
  if (event.type == "setFinish"){
    res = await db.collection("task").where({_id:event.id}).update({data:{type:"已完成"}})
  }
  if (event.type == "updateNotice"){
    res = await db.collection("notice").where({_id:"b00064a7606996490d38d01a7094fb3b"}).update({data:{notice:event.notice}})
  }
  return {event,openid: wxContext.OPENID,appid: wxContext.APPID,unionid: wxContext.UNIONID,env: wxContext.ENV,user: res}
}